package com.topscomm.gpm;

import com.topscomm.basic.exception.MessageException;
import com.topscomm.cbo.pojo.CboApprovalNodeInsEntity;
import com.topscomm.cbo.pojo.CboUserEntity;
import com.topscomm.cbo.service.impl.CboApprovalNodeInsService;
import com.topscomm.cbo.service.impl.CboUserService;
import com.topscomm.gpm.pojo.GpmProjectAppointmentEntity;
import com.topscomm.gpm.service.impl.GpmProjectTaskCloseService;
import com.topscomm.mis.constant.MisSystemConst;
import com.topscomm.mis.mobile.message.service.SendMessageService;
import com.topscomm.pub.util.*;
import com.topscomm.tap.ITapService;
import com.topscomm.tap.TapEntity;
import com.topscomm.tap.TapScript;
import com.topscomm.tap.common.DatacodeCache;
import com.topscomm.tap.common.TapSystemConst;
import org.springframework.security.core.parameters.P;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description TODO
 * @Classname SdScript
 * @Date 2021/12/8
 * @Created by jiangtao
 */
public class GpmScript extends TapScript {
	/**
	 * 生成App消息推送内容
	 *
	 * @author: jiangtao
	 * @date: 2020年4月29日上午8:26:25
	 * @params:
	 * @return_type: String
	 * @description:
	 * @modify:
	 */

	@Override
	public String geneAppContentSubmitNew(ITapService service, TapEntity entity) {
		// 审批节点实例
		CboApprovalNodeInsEntity nodeins = (CboApprovalNodeInsEntity) entity;
		// 组织系统消息内容
		StringBufferProxy msg = new StringBufferProxy();
		DatacodeCache datacodeCache = (DatacodeCache) SpringUtil.getBean("datacodeCache");
		String dispName = datacodeCache.getValueDynamic("CboApprovalFlowCategory", nodeins.getSourcetype(), "code",
				"name");
		String maintitle = nodeins.getMaintitle();
		String extendfield2 = nodeins.getExtendfield2();
		// maintitle只取时间字段
		if (!StringUtil.isEmpty(maintitle)) {
			int pos = maintitle.indexOf("于20");
			if (pos != -1) {
				maintitle = maintitle.substring(pos + 1, pos + 11);
			}
		}
		if (StringUtil.isEmpty(dispName)) {
			dispName = "业务申请";
		}
		if (nodeins.getState() == TapSystemConst.TapApprovalNodeStateEnum.UnApproved) {
			msg.appendSingle("您于{0}提交的{1}单据在“{2}”阶段被驳回！", maintitle, dispName, extendfield2);
		} else {
			msg.appendSingle("您于{0}提交的{1}单据在“{2}”阶段审批通过！", maintitle, dispName, extendfield2);
		}
		return msg.toString();
	}

	/**
	 * 生成手机端系统消息主题
	 *
	 * @author: jiangtao
	 * @date: 2020年4月29日上午8:28:24
	 * @params:
	 * @return_type: String
	 * @description:
	 * @modify:
	 */
	@Override
	public String geneSysSubjectSubmitNew(ITapService service, TapEntity entity) {
		StringBufferProxy msg = new StringBufferProxy();
		// 审批节点实例
		CboApprovalNodeInsEntity nodeins = (CboApprovalNodeInsEntity) entity;
		DatacodeCache datacodeCache = (DatacodeCache) SpringUtil.getBean("datacodeCache");
		String dispName = datacodeCache.getValueDynamic("CboApprovalFlowCategory", nodeins.getSourcetype(), "code",
				"name");
		if (StringUtil.isEmpty(dispName)) {
			dispName = "业务申请";
		}
		msg.appendSingle("{0}通知", dispName);
		return msg.toString();
	}

	/**
	 * 生成手机端系统消息内容
	 *
	 * @author: jiangtao
	 * @date: 2020年4月29日上午8:29:07
	 * @params:
	 * @return_type: String
	 * @description:
	 * @modify:
	 */
	@Override
	public String geneSysContentSubmitNew(ITapService service, TapEntity entity) {
		// 审批节点实例
		CboApprovalNodeInsEntity nodeins = (CboApprovalNodeInsEntity) entity;
		// 组织系统消息内容
		StringBufferProxy msg = new StringBufferProxy();
		DatacodeCache datacodeCache = (DatacodeCache) SpringUtil.getBean("datacodeCache");
		String dispName = datacodeCache.getValueDynamic("CboApprovalFlowCategory", nodeins.getSourcetype(), "code",
				"name");
		String maintitle = nodeins.getMaintitle();
		String extendfield2 = nodeins.getExtendfield2();
		// maintitle只取时间字段
		if (!StringUtil.isEmpty(maintitle)) {
			int pos = maintitle.indexOf("于20");
			if (pos != -1) {
				maintitle = maintitle.substring(pos + 1, pos + 11);
			}
		}
		if (StringUtil.isEmpty(dispName)) {
			dispName = "业务申请";
		}
		if (nodeins.getState() == TapSystemConst.TapApprovalNodeStateEnum.UnApproved) {
			msg.appendSingle("&nbsp;&nbsp;&nbsp;&nbsp;" + "您于{0}提交的{1}单据在“{2}”阶段被驳回！" + "<br>", maintitle, dispName,
					extendfield2);
		} else {
			msg.appendSingle("&nbsp;&nbsp;&nbsp;&nbsp;" + "您于{0}提交的{1}单据在“{2}”阶段审批通过！" + "<br>", maintitle, dispName,
					extendfield2);
		}
		String approvalopition = nodeins.getApprovalopinion();
		if (StringUtil.isEmpty(approvalopition)) {
			approvalopition = I18nUtil.getMessage("tap.common.not");
		}
		msg.appendSingle(
				"&nbsp;&nbsp;&nbsp;&nbsp;" + I18nUtil.getMessage("tap.cboapprovalnode.approvalcomments") + ":{0}<br>",
				approvalopition);
		msg.appendSingle("&nbsp;&nbsp;&nbsp;&nbsp;" + I18nUtil.getMessage("tap.date.senddate") + ":{0}<br>",
				DateUtil.Now());
		return msg.toString();
	}

//	/**
//	 * @Description:    审批节点启动发布待审批消息
//	 * @Author:         wuyuyang
//	 * @Return:         void
//	 * @exception:
//	 * @Date:           16/10/2023 下午7:44
//	 * @Version:        1.0
//	 **/
//	public void sendMessageBeforeApprove(ITapService service, TapEntity entity) {
//		// 审批节点实例
//		CboApprovalNodeInsEntity nodeins = (CboApprovalNodeInsEntity) entity;
//		String sourceid = ConvertUtil.convertToString(nodeins.getSourceid());
//		CboApprovalNodeInsService cboApprovalNodeInsService = (CboApprovalNodeInsService) SpringUtil
//				.getBean("cboApprovalNodeInsService");
//		CboUserService cboUserService = (CboUserService) SpringUtil
//				.getBean("cboUserService");
//		SendMessageService sendMessageService= (SendMessageService) SpringUtil.getBean("misPushMsgService");
//		// 消息内容
//		StringBufferProxy content = new StringBufferProxy();
//		content.appendSingle("您有一条新的单据需要审批，请登陆PC端进行办理!");
//		// 待办节点
//		StringBufferProxy sql = new StringBufferProxy();
//		sql.appendSingle("sourceid = '{0}' and sourcetype = '{1}' and state = 1 ", nodeins.getSourceid(), nodeins.getSourcetype());
//		List<CboApprovalNodeInsEntity> nodeInsEntities = cboApprovalNodeInsService.queryByWhere(sql.toString());
//		// 单人节点
//		if (nodeInsEntities.size() == 1) {
//			nodeins = nodeInsEntities.get(0);
//			// 组织系统消息内容
//			Map<String, Object> messageAppmap = new HashMap<String, Object>();
//			String userId = ConvertUtil.convertToString(nodeins.getApprovaluserid());
//			if (StringUtil.isEmpty(userId)) {
//				return;
//			}
//			CboUserEntity cboUserEntity = cboUserService.queryById(userId);
//			if (cboUserEntity == null){
//				return;
//			}
//			// App通知消息
//			messageAppmap.put("receivercode", cboUserEntity.getCode());
//			messageAppmap.put("receivername", cboUserEntity.getName());
//			messageAppmap.put("content", content.toString());
//			messageAppmap.put("extendfield1", "通用项目管理系统审批提醒");
//			messageAppmap.put("sendtype", "0");
//			messageAppmap.put("sourcetype", nodeins.getSourcetype());
//			messageAppmap.put("sourceid",  nodeins.getSourceid());
//			messageAppmap.put("messagetype", MisSystemConst.MobileMessageType.APPROVE_TYPE);
//			sendMessageService.sendAppMessage(messageAppmap, ConvertUtil.convertToString(nodeins.getId()));
//			// 系统通知消息
//			Map<String, Object> messageSysAppmap = new HashMap<String, Object>();
//			messageSysAppmap.put("receivercode", cboUserEntity.getCode());
//			messageSysAppmap.put("receivername", cboUserEntity.getName());
//			messageSysAppmap.put("content", content.toString());
//			messageSysAppmap.put("subject", "通用项目管理系统审批提醒");
//			messageSysAppmap.put("ishtm", 0);
//			messageSysAppmap.put("sourcetype", nodeins.getSourcetype());
//			messageSysAppmap.put("sourceid", nodeins.getSourceid());
//			messageSysAppmap.put("messagetype", MisSystemConst.MobileMessageType.APPROVE_TYPE);
//			sendMessageService.sendSysMessage(messageSysAppmap,ConvertUtil.convertToString(nodeins.getId()));
//		} else { // 多人节点
//            String userids = nodeInsEntities.stream().map(m -> "'" + m.getApprovaluserid() + "'").collect(Collectors.joining(", "));
//			List<CboUserEntity> cboUserEntities = cboUserService.queryByIds(userids);
//			sql.clear();
//			sql.appendSingle("select id, code, name from cbouser where id in ({0})", userids);
//			List<Map<String, Object>> res = cboUserService.selectExecute(sql.toString());
//			nodeInsEntities.stream().forEach(m -> {
//				res.stream().forEach(n -> {
//					if (ConvertUtil.convertToString(m.getApprovaluserid()).equals(ConvertUtil.convertToString(n.get("id")))) {
//						Map<String, Object> messageAppmap = new HashMap<String, Object>();
//						// App通知消息
//						messageAppmap.put("receivercode", ConvertUtil.convertToString(n.get("code")));
//						messageAppmap.put("receivername", ConvertUtil.convertToString(n.get("name")));
//						messageAppmap.put("content", content.toString());
//						messageAppmap.put("extendfield1", "通用项目管理系统审批提醒");
//						messageAppmap.put("sendtype", "0");
//						messageAppmap.put("sourcetype", m.getSourcetype());
//						messageAppmap.put("sourceid",  m.getSourceid());
//						messageAppmap.put("messagetype", MisSystemConst.MobileMessageType.APPROVE_TYPE);
//						sendMessageService.sendAppMessage(messageAppmap, ConvertUtil.convertToString(m.getId()));
//						// 系统通知消息
//						Map<String, Object> messageSysAppmap = new HashMap<String, Object>();
//						messageSysAppmap.put("receivercode", ConvertUtil.convertToString(n.get("code")));
//						messageSysAppmap.put("receivername", ConvertUtil.convertToString(n.get("name")));
//						messageSysAppmap.put("content", content.toString());
//						messageSysAppmap.put("subject", "通用项目管理系统审批提醒");
//						messageSysAppmap.put("ishtm", 0);
//						messageSysAppmap.put("sourcetype", m.getSourcetype());
//						messageSysAppmap.put("sourceid", m.getSourceid());
//						messageSysAppmap.put("messagetype", MisSystemConst.MobileMessageType.APPROVE_TYPE);
//						sendMessageService.sendSysMessage(messageSysAppmap,ConvertUtil.convertToString(m.getId()));
//					}
//				});
//			});
//
//		}
//
//	}


}
